package com.tradeplus.admin.dao;

import com.tradeplus.admin.model.Group;
import com.tradeplus.admin.model.GroupMember;
import com.tradeplus.admin.model.GroupPrivilege;
import java.util.List;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class GroupDaoImpl implements GroupDao {
    
    private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public void deleteGroup(Long groupId) {
        hibernateTemplate.delete(hibernateTemplate.get(Group.class, groupId));
    }

    @Override
    public List<Group> loadGroup() {
        return hibernateTemplate.find("from Group");
    }

    @Override
    public Group loadGroupById(Long groupId) {
        return (Group) hibernateTemplate.get(Group.class, groupId);
    }

    @Override
    public void saveGroup(Group group) {
        hibernateTemplate.merge(group);
    }

    @Override
    public GroupMember getGroupByMemberId(int memberId) {
        return (GroupMember) hibernateTemplate.getSessionFactory().openSession().createCriteria(GroupMember.class).add(Restrictions.eq("member.id", memberId));
    }

    @Override
    public GroupPrivilege getGroupByPrivilegeId(int privilegeId) {
        return (GroupPrivilege) hibernateTemplate.getSessionFactory().openSession().createCriteria(GroupPrivilege.class).add(Restrictions.eq("privilege.id", privilegeId));
    }
    
    
}
